// @ts-nocheck
import React, { Component } from "react";
import { Toast } from "antd-mobile";
import AMapLoader from "@amap/amap-jsapi-loader";
import { Map_Key } from "@/utils/config";
import "./index.less";

interface IState {
	value: string;
	address: string;
	formItem: {
		content?: string;
		addressDetailedUser?: string;
		type?: string[];
		fileFront?: string;
		address?: string;
		title?: string;
	};
	addressItem: IAddressItem;
}

type IProps = {};

const DEFALTE_ADDRESS: IAddressItem = {
	hadress: "", //详细地址
	longitude: -1, //经度
	latitude: -1, //纬度
};

class ReadOnlyMap extends Component<IProps, IState> {
	map: {};
	marker: any;
	geocoder: any;
	geolocation: any;
	cityCode: string | null;
	current: any;
	placeSearch: any;
	auto: any;
	heaf: string;
	constructor() {
		super();
		this.map = {};
		this.marker = null;
		this.geocoder = null;
		this.geolocation = null;
		this.cityCode = "028";
		this.current = null;
		this.placeSearch = null;
		this.auto = null;
		this.heaf = "";
		this.state = {};
	}
	componentDidMount() {
		const point = this.props?.point;
		AMapLoader.load({
			key: Map_Key, // 申请好的Web端开发者Key，首次调用 load 时必填
			version: "2.0", // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
			plugins: ["AMap.Geocoder", "AMap.PlaceSearch", "AMap.AutoComplete", "AMap.Marker", "AMap.Geolocation"], // 需要使用的的插件列表，如比例尺'AMap.Scale'等
		})
			.then((AMap) => {
				const that = this;
				this.map = new AMap.Map("container", {
					zoom: 15,
					resizeEnable: true,
					center: [106.63, 30.46],
				});
				// this.map.setCity("简阳市");
				// this.map.getCity((result) => {
				// 	{
				// 		that.cityCode = result.citycode;
				// 		that.placeSearch.setCity(result.citycode);
				// 		that.auto.setCity(result.citycode);
				// 	}
				// });

				this.geolocation = new AMap.Geolocation({
					convert: true,
					enableHighAccuracy: true,
					timeout: 10000, // 10秒钟超时
					maximumAge: 1000 * 60 * 5,
					showMarker: true,
					panToLocation: false,
					zoomToAccuracy: false,
					GeoLocationFirst: true,
					getCityWhenFail: true,
					needAddress: true,
					useNative: true,
				});

				this.map.addControl(this.geolocation);
				this.geolocation.getCurrentPosition(this.getCurrentPosition);

	
				this.marker = new AMap.Marker({
					position: [point?.longitude, point?.latitude],
					map: this.map,
				});
				this.map.setCenter([point?.longitude, point?.latitude]);
			})
			.catch((e) => {
				console.error(e);
			});
	}

	render() {
		return (
			<>
				<div
					id="container"
					className="map"
					style={{ height: "100%" }}></div>
			</>
		);
	}
}
export default ReadOnlyMap;
